package com.lzs.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lzs.entity.Menu;
import com.lzs.entity.Role;
import com.lzs.system.service.MenuService;
import com.lzs.system.service.RoleService;
import com.lzs.util.R;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author 刘中帅
 * @since 2020-11-24
 */
@RestController
@RequestMapping("/system/role")
@ApiModel(value = "角色管理")
public class RoleController {
    @Autowired
   private RoleService roleService;
    @Autowired
    private MenuService menuService;
    @GetMapping("/selectRoleTree")
    @ApiOperation(value = "角色树")
    public R selectRoleTree(){
        List<Integer> list=new ArrayList<>();
        list.add(24);
        R data = R.ok().data("roleTree", roleService.selectRoleTree());
        return data;
    }

    @ApiOperation(value = "角色分页查询")
    @PostMapping("/selectRolePage/{page}/{limit}")
    public IPage<Role> selectRolePage(@PathVariable Integer page,@PathVariable Integer limit,@RequestBody Role role){
        IPage<Role> rolePage = roleService.selectRolePage(page, limit, role);
        return rolePage;
    }
    // 新增修改
    @PostMapping("/insertOrUpdateRole")
    @ApiOperation(value = "角色新增修改")
    public  R insertOrUpdateRole(@RequestBody Role role){
        return roleService.insertOrUpdateRole(role);
    }
    // 根据Id删除
    @DeleteMapping("/deleteRole/{roleId}")
    @ApiOperation(value = "角色删除")
    public R deleteRole(@PathVariable Long roleId){
        boolean b = roleService.removeById(roleId);
        if (b){
            return R.ok().message("删除成功");
        }
        return R.ok().message("删除失败");
    }

    @GetMapping("/selectPerByRoleId/{roleId}")
    @ApiOperation(value = "角色权限树")
    public R selectPerByRoleId(@PathVariable Long roleId){
        Menu menu=new Menu();
        // 权限树
        List<Menu> menus = menuService.selectMenuTreeByRoleId(menu,roleId);
        // 当前角色Id 具有的权限ID
        List<Long> roleIds = roleService.selectMenuByRoleId(roleId);
//        List<Long> menuIds=new ArrayList<>();
//        for (Long menuId:roleIds){
//            boolean yeZi = roleService.isYeZi(menuId, menus);
//            if (yeZi){
//                menuIds.add(menuId);
//            }
//        }
        R r = R.ok().data("menus", menus).data("menuIds", roleIds);
        return r;
    }
    // 改变当前角色权限
    @PostMapping("/updateMenuByRoleId/{roleId}")
    @ApiOperation(value = "角色分配权限")
    public R updateMenuByRoleId(@PathVariable Long roleId,@RequestBody List<Long> menus){
        R r = roleService.updateMenuByRoleId(roleId,menus);
        return r;
    }

    @GetMapping("/findByRoleId/{roleId}")
    @ApiOperation(value = "修改时回显数据")
    public R findByRoleId(@PathVariable Long roleId){
        return roleService.findByRoleId(roleId);
    }

//    @PostMapping("/selectAllRolePage/{page}/{limit}")
//    @ApiOperation(value = "角色分页查询")
//    public Page<Role> selectAllRolePage(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody Role role){
//        Page<Role> rolePage = roleService.selectAllRolePage(page, limit, role);
//        return rolePage;
//    }
}

